// pages/user/user.js
const { globalData } = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    userInfo: {},
    imagePrefix: globalData.imageUrl,
    waterElecAuth: false, // 水电工
    companyAuth: false, // 装饰公司
    designerAuth: false, // 设计师
    projectManagerAuth: false, // 项目经理
    agentAuth: false, // 代理商
    agentPayAuth: false, // 经销商
    isMerchantEmp: false,
    merchantInfo: {},
    otherList: [
      {
        name: "我的好友",
        url: "/pages/my-team/index",
        icon: "/assets/images/icon/icon-13.png",
      },
      // {
      //   name: "会员推荐",
      //   url: "/pages/recommend-auth/index",
      //   icon: "/assets/images/icon/icon-14.png",
      // },
      {
        name: "联系客服",
        url: "/pages/customer-service/index",
        icon: "/assets/images/icon/icon-15.png",
      },
      {
        name: "常见问题",
        url: "/pages/fqa/fqa",
        icon: "/assets/images/icon/icon-16.png",
      },
      {
        name: "关于我们",
        url: "/pages/company-profile/company-profile",
        icon: "/assets/images/icon/icon-17.png",
      },
      {
        name: "我的VR",
        url: "/vr/pages/vr-list/vr-list",
        icon: "/assets/images/icon/vr.png",
      },
      {
        name: "网点",
        url: "/pages/store-map/index",
        icon: "/assets/images/icon/icon-12.png",
      },
      {
        name: "优惠券",
        url: "/map/pages/decora/index",
        icon: "/assets/images/icon/icon-2.png",
      },
      {
        name: "分享佣金",
        url: "/credits/pages/share-record/index",
        icon: "/assets/images/icon/icon-2.png",
      }
    ],
    showAuth: false,
    installData: {
        num1: 0,
        num2: 0,
        num3: 0
    }
  },

  getUserInfo() {
    wx.ajax.post("queryUserInfo.action", {}, true).then((res) => {
      if (res.success) {
        globalData.userInfo = res.result
        if("1" == res.result.vr_str) {
                  this.setData({
                    otherList: [
                        {
                          name: "我的好友",
                          url: "/pages/my-team/index",
                          icon: "/assets/images/icon/icon-13.png",
                        },
                        // {
                        //   name: "会员推荐",
                        //   url: "/pages/recommend-auth/index",
                        //   icon: "/assets/images/icon/icon-14.png",
                        // },
                        {
                          name: "联系客服",
                          url: "/pages/customer-service/index",
                          icon: "/assets/images/icon/icon-15.png",
                        },
                        {
                          name: "常见问题",
                          url: "/pages/fqa/fqa",
                          icon: "/assets/images/icon/icon-16.png",
                        },
                        {
                          name: "关于我们",
                          url: "/pages/company-profile/company-profile",
                          icon: "/assets/images/icon/icon-17.png",
                        },
                        {
                          name: "我的VR",
                          url: "/vr/pages/vr-list/vr-list",
                          icon: "/assets/images/icon/vr.png",
                        },
                        {
                          name: "网点",
                          url: "/map/pages/store-map/index",
                          icon: "/assets/images/icon/icon-12.png",
                        },
                        {
                          name: "优惠券",
                          url: "/map/pages/decora/index",
                          icon: "/assets/images/icon/icon-2.png",
                        },
                        {
                          name: "分享佣金",
                          url: "/credits/pages/share-record/index",
                          icon: "/assets/images/icon/icon-2.png",
                        }
                      ]
                   });
        } else {
            this.setData({
                otherList: [
                    {
                      name: "我的好友",
                      url: "/pages/my-team/index",
                      icon: "/assets/images/icon/icon-13.png",
                    },
                    // {
                    //   name: "会员推荐",
                    //   url: "/pages/recommend-auth/index",
                    //   icon: "/assets/images/icon/icon-14.png",
                    // },
                    {
                      name: "联系客服",
                      url: "/pages/customer-service/index",
                      icon: "/assets/images/icon/icon-15.png",
                    },
                    {
                      name: "常见问题",
                      url: "/pages/fqa/fqa",
                      icon: "/assets/images/icon/icon-16.png",
                    },
                    {
                      name: "关于我们",
                      url: "/pages/company-profile/company-profile",
                      icon: "/assets/images/icon/icon-17.png",
                    },
                    {
                      name: "网点",
                      url: "/map/pages/store-map/index",
                      icon: "/assets/images/icon/icon-12.png",
                    },
                    {
                      name: "优惠券",
                      url: "/map/pages/decora/index",
                      icon: "/assets/images/icon/icon-2.png",
                    },
                    {
                      name: "分享佣金",
                      url: "/credits/pages/share-record/index",
                      icon: "/assets/images/icon/icon-2.png",
                    }
                  ]
               });
        }
        this.setData({
          userInfo: res.result,
          showAuth: !res.result.wx_nick_name,
          loading: false,
        });

        this.handleCheck(res.result);
        this.requestUserInstallsDetail()
      }
      wx.stopPullDownRefresh()
    });
  },


  handleCheck(result) {
    let setData = {}

    setData.isMerchantEmp = result.isMerchantEmp
    // 判断是否门店员工
    if (result.isMerchantEmp) {
      // 是
      setData.merchantInfo = result.merchantInfo
      // this.setData({
      //   isMerchantEmp: result.isMerchantEmp,
      //   merchantInfo: result.merchantInfo,
      // });

      wx.setNavigationBarTitle({
        title: result.merchantInfo.merchant_name,
      })
    } else {
      // 否
      // this.setData({
      //   isMerchantEmp: result.isMerchantEmp,
      // });
      wx.setNavigationBarTitle({
        title: '我的'
      })
    }

    let typeList = ['', 'agentAuth', 'agentPayAuth', 'agentPayAuth', 'companyAuth', 'waterElecAuth', 'designerAuth', '', 'projectManagerAuth']

    result.userTypeList.forEach(item => {
      let typeKey = typeList[item]
      if (typeKey) {
        setData[typeKey] = true
      }
    })


    // 判断权限
    // if (result.userTypeList.indexOf("1") > -1) {
    //   // 代理商
    //   this.setData({
    //     agentAuth: true,
    //   });
    // }

    // if (
    //   result.userTypeList.indexOf("2") > -1 ||
    //   result.userTypeList.indexOf("3") > -1
    // ) {
    //   // 经销商
    //   this.setData({
    //     agentPayAuth: true,
    //   });
    // }

    // if (result.userTypeList.indexOf("4") > -1) {
    //   // 装饰公司
    //   this.setData({
    //     companyAuth: true,
    //   });
    // }

    // if (result.userTypeList.indexOf("5") > -1) {
    //   // 水电工
    //   this.setData({
    //     waterElecAuth: true,
    //   });
    // }

    // if (result.userTypeList.indexOf("6") > -1) {
    //   设计师
    //   this.setData({
    //     designerAuth: true,
    //   });
    // }

    // if (result.userTypeList.indexOf("8") > -1) {
    //   // 项目经理
    //   this.setData({
    //     projectManagerAuth: true,
    //   });
    // }


    this.setData(setData)
  },

  handleToPage(event) {
    // console.log(event);
    const { url, type, title, from } = event.currentTarget.dataset;

    if (!url) return;

    const { isMerchantEmp, merchantInfo } = this.data;

    const path = `${url}?&title=${title || ""}&type=${type || ""}&id=${
      merchantInfo.id || ""
    }&isMerchantEmp=${isMerchantEmp ? "1" : "0"}&from=${from}`;

    wx.navigateTo({
      url: path,
    });
  },

  hadnleToEditUser() {
    wx.navigateTo({
      url: "/pages/edit-user/index",
    });
  },

  successHandle(event) {
    this.setData({
      userInfo: event.detail,
      showAuth: false,
    });
  },
  requestUserInstallsDetail() {
    wx.ajax.post('userInstallsDetail.action').then(res => {
        this.setData({
            installData: res.result
        })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo();
    // 判断用户是否登录 未登录跳转到登录页面
    // const userInfo = wx.getStorageSync("userInfo");
    // if (!userInfo || !userInfo.name) {
    //   this.getUserInfo();
    // } else {
    //   this.setData({
    //     userInfo: userInfo,
    //     showAuth: !userInfo.wx_nick_name,
    //   });
    //   this.handleCheck(userInfo);
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUserInfo();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
